package com.itheima79.googleplay.ui.activity;

import com.itheima79.googleplay.R;
import com.itheima79.googleplay.bean.AppInfo;
import com.itheima79.googleplay.global.ImageLoaderOptions;
import com.itheima79.googleplay.holder.AppDesHolder;
import com.itheima79.googleplay.holder.AppDownloadHolder;
import com.itheima79.googleplay.holder.AppInfoHolder;
import com.itheima79.googleplay.holder.AppSafeHolder;
import com.itheima79.googleplay.holder.AppScreenHolder;
import com.itheima79.googleplay.http.Api;
import com.itheima79.googleplay.http.HttpUtil;
import com.itheima79.googleplay.ui.fragment.LoadingPage;
import com.itheima79.googleplay.util.CommonUtil;
import com.itheima79.googleplay.util.JsonUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.format.Formatter;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.TextView;

public class AppDetailActivity extends ActionBarActivity{
	private String packageName;
	private LinearLayout holder_container;
	private ScrollView scrollview;
	private FrameLayout fl_download;
	
	//app info模块的控件
	private AppInfoHolder appInfoHolder;
	private AppSafeHolder appSafeHolder;
	private AppScreenHolder appScreenHolder;
	private AppDesHolder appDesHolder;
	private AppDownloadHolder appDownloadHolder;
	
	private AppInfo appInfo;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//1.接收数据
		packageName = getIntent().getStringExtra("packageName");
		//2.设置ActionBar
		setActionBar();
		//3.使用LoadingPage管理Activity的数据加载和界面切换
		LoadingPage loadingPage = new LoadingPage(this) {
			@Override
			public Object loadData() {
				return requestData();
			}
			@Override
			public View createSuccessView() {
				View view = View.inflate(AppDetailActivity.this, R.layout.activity_app_detail, null);
				holder_container = (LinearLayout) view.findViewById(R.id.holder_container);
				scrollview = (ScrollView) view.findViewById(R.id.scrollview);
				fl_download = (FrameLayout) view.findViewById(R.id.fl_download);
				//1.初始化app info模块
				appInfoHolder = new AppInfoHolder();
				holder_container.addView(appInfoHolder.getHolderView());
				//2.初始化app safe模块
				appSafeHolder = new AppSafeHolder();
				holder_container.addView(appSafeHolder.getHolderView());
				//3.初始化app screen模块
				appScreenHolder = new AppScreenHolder();
				appScreenHolder.attachActivity(AppDetailActivity.this);
				holder_container.addView(appScreenHolder.getHolderView());
				//4.初始化app des模块
				appDesHolder = new AppDesHolder();
				holder_container.addView(appDesHolder.getHolderView());
				appDesHolder.setScrollView(scrollview);
				//5.初始化app download模块
				appDownloadHolder = new AppDownloadHolder();
				fl_download.addView(appDownloadHolder.getHolderView());
				
				return view;
			}
		};
		setContentView(loadingPage);
	}
	
	/**
	 * 请求数据的方法
	 * @return
	 */
	protected Object requestData(){
		String url = String.format(Api.Detail,packageName);
		String result = HttpUtil.get(url);
		appInfo = JsonUtil.parseJsonToBean(result, AppInfo.class);
		
		if(appInfo!=null){
			//更新UI
			CommonUtil.runOnUIThread(new Runnable() {
				@Override
				public void run() {
					updateUI();
				}
			});
		}
		
		return appInfo;
	}
	/**
	 * 更新Ui的方法
	 */
	protected void updateUI() {
		//1.绑定app info模块的数据
		appInfoHolder.bindData(appInfo);
		//2.绑定app safe模块的数据
		appSafeHolder.bindData(appInfo);
		//3.绑定app screen模块的数据
		appScreenHolder.bindData(appInfo);
		//4.绑定app des模块的数据
		appDesHolder.bindData(appInfo);
		//5.绑定app download模块的数据
		appDownloadHolder.bindData(appInfo);
	}
	/**
	 * 设置ActionBar
	 */
	private void setActionBar() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setTitle(getString(R.string.app_detail));
		//显示home按钮
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowHomeEnabled(true);
	}
	/**
	 * 点击ActionBar的home按钮的时候结束当前界面
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
